<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item v-for="(item, index) in breadList" :key="index" :to="{ path: item.path }">{{ item.meta.title
        }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" status-icon>
          <el-form-item label="门票名" prop="ticketName">
            <el-input v-model="ruleForm.ticketName" />
          </el-form-item>
          <el-form-item label="门票类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio :label="0">儿童票</el-radio>
              <el-radio :label="1">成人票</el-radio>
              <el-radio :label="2">团体票</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="ruleForm.price" type="price" />
          </el-form-item>
    <el-form-item label="景点" >
      <el-select  v-model="ruleForm.AttractionId" filterable placeholder="请选择相关景点">
      <el-option
        v-for="item in atts"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      />
    </el-select>
    </el-form-item>
          <el-form-item label="详细" prop="desc">
            <el-input v-model="ruleForm.desc" :rows="2" type="textarea"/>
          </el-form-item>
          <el-form-item label="有效日期" prop="validDate">
            <el-date-picker v-model="ruleForm.validDate" type="datetime" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitForm(ruleFormRef)">新增</el-button>
        <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { useRouter, useRoute } from 'vue-router'
import { ref, reactive, watch } from "vue";
import request from "../../utils/request";
import { ElNotification } from "element-plus";
export default {
  setup() {
    let breadList = [] // 路由集合
    const route = useRoute()
    const atts = ref([]);//景点集合
    const getBreadcrumb = (url) => {
      let matched = url.matched;
      //如果不是首页
      if ("Home" == matched[0].name) {
        matched[0].meta = { title: "系统首页" }
      }
      breadList = matched;
    }
    watch(route, (newValue, oldValue) => {
      // console.log(222222,newValue)
      getBreadcrumb(newValue)
    }, { deep: true, immediate: true });

    const resetForm = (formEl) => {
      if (!formEl) return;
      formEl.resetFields();
    };
    const submitForm = async (formEl) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          // console.log("提交更新", fields);
          request
            .request({
              url: "/ticket/add",
              method: "post",
              data: {
                ticketName: ruleForm.ticketName,
                type: ruleForm.type,
                price: ruleForm.price,
                AttractionId:ruleForm.AttractionId,
                desc: ruleForm.desc,
                validDate: ruleForm.validDate,
              },
            })
            .then((res) => {
              if (res.data.code == 200) {
                ElNotification({
                  title: "新增成功",
                  message: "成功更新用户信息",
                });
              } else {
                ElNotification({
                  title: "新增失败",
                  message: "请联系管理员检查失败原因",
                });
              }
            })
            .catch((e) => {
              ElNotification({
                title: "新增失败",
                message: "请联系管理员检查失败原因",
              });
            });
        } else {
          ElNotification({
            title: "校验失败",
            message: "请重新填写信息",
          });
        }
      });
    };

    const ruleForm = reactive({
      ticketName: "",
      type: 1,
      price: "",
      AttractionId:"",
      desc: "",
      validDate: "",
    });
    const rules = reactive({
      ticketName: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      type: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
      ],
      price: [
        {
          required: true,
          message: "请输入电话号码",
          trigger: "blur",
        },
      ],
      desc: [
        {
          required: false,
          message: "请输入地址",
          trigger: "blur",
        },
      ],
      validDate: [
        {
          required: true,
          message: "请选择用户类型",
          trigger: "change",
        },
      ],

    });
    // 获取景点数据
    const getAttraction = () => {
        request
          .request({
            url: "/attraction/getAllAttraction",
            method: "get",
          })
          .then((res) => {
            //获取当前页的数据
            atts.value = res.data.data;
      console.log(JSON.stringify(atts)+"所有景点");
    })
    .catch((e) => {
      console.log(e);
    });
};
getAttraction();
    const ruleFormRef = ref();
    const centerDialogVisible = ref(false);
    return {
      breadList,
      resetForm,
      ruleFormRef,
      submitForm,
      ruleForm,
      rules,
      centerDialogVisible,
      getAttraction,
      atts
    };
  },
};

</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
    